/**
 * 表单值域字典表管理初始化
 */
var SdsCusIndexField = {
    id: "SdsCusIndexFieldTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsCusIndexField.initColumn = function () {
    return [[
        {title: '条目代码', field: 'code', width: 100, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox',
                options:{required:true}
        }},
        {title: '数据源', field: 'datasource', width: 200, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                for (let j = 0, len = dataMap.length; j < len; j++) {
                    if (value == dataMap[j].value) {
                        return dataMap[j].text;
                        break;
                    }
                }
            },
            editor: {
                type: 'combotree',
                options: {
                    url: '/sds/sdsstanddatasource/getDataSourceTree'
                }
            }
        },
        {
            title: '标化字典', field: 'dic', width: 200, align: 'center', valign: 'middle',id:'dic',
            formatter: function (value, row, index) {
                for (let j = 0, len = dicMap.length; j < len; j++) {
                    if (value == dicMap[j].value) {
                        return dicMap[j].text;
                        break;
                    }
                }
            },
            editor: {
                type: 'combotree',
                options: {
                    url: '/sds/sdsstanddic/getDicTree/' + "0"
                }
            }
        },
        {
            title: '取值时机', field: 'valueTime', width: 200, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                for (let j = 0, len = valueTimeMap.length; j < len; j++) {
                    if (value == valueTimeMap[j].id) {
                        return valueTimeMap[j].text;
                        break;
                    }
                }
            },
            editor: {
                type: 'combobox',
                options: {
                    valueField: 'id',
                    textField: 'text',
                    editable: false,
                    data: valueTimeMap
                }
            }
        }
    ]];
};

/**
 * 检查是否选中
 */
SdsCusIndexField.check = function () {
    var selected = $('#' + this.id).datagrid('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsCusIndexField.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加新一行表格
 */
SdsCusIndexField.addFItem = function () {
    var rowNum=$('#' + SdsCusIndexField.id).datagrid("getRows").length;
    //添加一行
    $('#' + SdsCusIndexField.id).datagrid('appendRow',{
        code: '',
        datasource: '',
        dic: '',
        valueTime: '',
        itemType: 1,
        entityId: $("#entityId").val(),
        indexHeaderId:$("#cusIndexHeaderId").val()
    });
    var rowIndex= rowNum;
    $('#' + SdsCusIndexField.id).datagrid('selectRow', rowIndex).datagrid('beginEdit', rowIndex);
};

/**
 * 保存数据到库表
 */
SdsCusIndexField.saveFItem = function () {
    //保存完毕后，同步数据
    var operation_save = function(){
        SdsCusIndexField.table.acceptChanges();//调用datagrid自带的方法，确认其修改 然后保存至数据库
        var rows =  $('#' + SdsCusIndexField.id).datagrid('getRows');
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            console.log(row)
            if (row != undefined && row != {}) {
                if (row.id != undefined) {
                    //update Row
                    delete row.updateDate;
                    delete row.updateUser;
                    Sys.ajax(Sys.backendPath + "/sds/sdscusindexheader/updateItem", row, function (data) {
                        Sys.success("修改成功!");
                        SdsCusIndexField.table.refresh();
                    },function(data){
                        Sys.error("修改失败!" + data.responseJSON.message + "!");
                    }, "json", false, "修改项目保存至库中..");
                } else {
                    //add Row
                    Sys.ajax(Sys.backendPath + "/sds/sdscusindexheader/addItem", row, function (data) {
                        Sys.success("添加成功!");
                        SdsCusIndexField.table.refresh();
                    },function(data){
                        Sys.error("添加失败!" + data.responseJSON.message + "!");
                    }, "json", false, "新增项目保存至库中..");
                }
            }
        }
    };
    Sys.confirm("是否保存表格数据至数据库?",operation_save);
};

/**
 * 删除行同时删除数据库表数据
 */
SdsCusIndexField.delFItem = function () {
    if (SdsCusIndexField.check()) {
        var operation = function(){
            if(SdsCusIndexField.seItem.id!=null&&SdsCusIndexField.seItem.id!=""){
                var ajax = new $ax(Sys.backendPath + "/sds/sdscusindexheader/deleteItem", function (data) {
                    Sys.success("删除成功!");
                    SdsCusIndexField.table.refresh();
                }, function (data) {
                    Sys.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("sdscusIndexFieldId",SdsCusIndexField.seItem.id);
                ajax.start();
            }else{
                SdsCusIndexField.table.delRow();
            }
        };
        Sys.confirm("是否删除该行数据?",operation);
    }
};

/**
 * 保存表达式
 */
SdsCusIndexField.editExpress = function () {
    //提交信息
    var ajax = new $ax(Sys.backendPath + "/sds/sdscusindexheader/updateExpress", function (data) {
        Sys.success("保存成功!");
    }, function (data) {
        Sys.error("保存失败!" + data.responseJSON.message + "!");
    });
    var express = encodeURIComponent($("#express").val());
    var cusIndexHeaderId = $("#cusIndexHeaderId").val();
    ajax.set("express",express);
    ajax.set("cusIndexHeaderId",cusIndexHeaderId);
    ajax.start();
};

/**
 * 查询表单值域字典表列表
 */
SdsCusIndexField.search = function () {
    var queryData = {};
    queryData['cusIndexHeaderId'] = $("#cusIndexHeaderId").val();
    queryData['itemType'] = 1;
    SdsCusIndexField.table.refresh({queryParams: queryData});
};

$(function () {
    var cusIndexHeaderId = $("#cusIndexHeaderId").val();
    var defaultColunms = SdsCusIndexField.initColumn();
    var table = new HISUITable(SdsCusIndexField.id, Sys.backendPath + '/sds/sdscusindexheader/listItem/' + cusIndexHeaderId + "/1", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-90);
    SdsCusIndexField.table = table.init();

});

